#region "StringExtensions"

/*
 * ####     Developer Name : Wang Yucai
 * 
 * ####     Development Tool : Microsoft VisualStudio 2010 Ultimate Edition
 * 
 * ####     Create Time : 2014/7/8 13:37:06
 * 
 * ####     Namespace : System
 * 
 * ####     Type Name : StringExtensions
 * 
 * ####     Full Name : System.StringExtensions
 * 
 * ####     Machine Name : GLCHQWYCWINW7
 * 
 * ####     UserName : GUOCOLAND/wangyucai
 * 
 * ####     CLR Version : 4.0.30319.18444
 * 
 * ####     Target Framework Version : 4.0
 */

#endregion

using System.Diagnostics;
using System.Text;
using Masterduner.SourceStudio.Practices.DevelopmentLibrary;

namespace System
{
    /// <summary>
    /// <para>System.StringExtensions</para>
    /// <para>
    /// 为<see cref="String"/>提供的扩展方法。
    /// </para>
    /// </summary>
    /// <remarks>
    /// <para>Target Framework Version : 4.0</para>
    /// </remarks>
    public static class StringExtensions
    {
        private static readonly TrueExpression trueExpr = ((args) => { throw new NullReferenceExceptionWrapper(); });

        #region IsNullOrEmpty
        /// <summary>
        /// 验证字符串<paramref name="current"/>是否为空指针或者空字符串。
        /// </summary>
        /// <param name="current">字符串。</param>
        /// <returns>如果<paramref name="current"/>等于空指针或者空字符串则返回true；否则返回false。</returns>
        static public bool IsNullOrEmpty(this string current)
        {
            bool isNullOrEmpty = current.IsNull() || current.Equals("");
            Debug.WriteLine("StringExtensions::IsNullOrEmpty::current::is {0}", isNullOrEmpty ? "null or empty" : "not null or empty");
            return isNullOrEmpty;
        }
        #endregion

        #region IsEmptyOrWriteSpaces
        /// <summary>
        /// 验证字符串<paramref name="current"/>是否等于空指针、空字符串或者空白符。
        /// </summary>
        /// <param name="current">字符串。</param>
        /// <returns>如果<paramref name="current"/>等于空指针、空字符串或者空白符，则返回true；否则返回false。</returns>
        static public bool IsEmptyOrWriteSpaces(this string current)
        {
            bool isEmptyOrSpaces = current.IsNullOrEmpty() || current.Trim().Equals("");
            Debug.WriteLine("StringExtensions::IsEmptyOrWritespaces::current::is {0}", isEmptyOrSpaces ? "empty or whitespaces" : "not empty or whitespaces");
            return isEmptyOrSpaces;
        }
        #endregion

        #region ToBoolean
        /// <summary>
        /// 将字符串转换成等效的<see cref="Boolean"/>值。
        /// </summary>
        /// <param name="current">等效的[true]或者[false]字符串。</param>
        /// <returns><see cref="Boolean"/>值。</returns>
        static public bool ToBoolean(this string current)
        {
            IfElse.Do(current.IsEmptyOrWriteSpaces(), trueExpr);
            try
            {
                return bool.Parse(current.ToLower());
            }
            catch (Exception ex)
            {
                throw new ArgumentsExceptionWrapper(ex);
            }
        }
        #endregion

        #region ToDateTime
        /// <summary>
        /// 将字符串转换成等效的<see cref="DateTime"/>值。
        /// </summary>
        /// <param name="current">字符串。</param>
        /// <returns><see cref="DateTime"/>类型值。</returns>
        static public DateTime ToDateTime(this string current)
        {
            IfElse.Do(current.IsEmptyOrWriteSpaces(), trueExpr);
            try
            {
                return DateTime.Parse(current);
            }
            catch (Exception ex)
            {
                throw new ArgumentsExceptionWrapper(ex);
            }
        }
        #endregion

        #region ToGuid
        /// <summary>
        /// 将字符串转换成等效的<see cref="Guid"/>值。
        /// </summary>
        /// <param name="current">字符串。</param>
        /// <returns><see cref="Guid"/>类型值。</returns>
        static public Guid ToGuid(this string current)
        {
            IfElse.Do(current.IsEmptyOrWriteSpaces(), trueExpr);
            try
            {
                return Guid.Parse(current);
            }
            catch (Exception ex)
            {
                throw new ArgumentsExceptionWrapper(ex);
            }
        }
        #endregion

        #region ToInt32
        /// <summary>
        /// 将字符串转换成等效的<see cref="Int32"/>值。
        /// </summary>
        /// <param name="current">字符串。</param>
        /// <returns><see cref="Int32"/>类型值。</returns>
        static public int ToInt32(this string current)
        {
            IfElse.Do(current.IsEmptyOrWriteSpaces(), trueExpr);
            try
            {
                return int.Parse(current);
            }
            catch (Exception ex)
            {
                throw new ArgumentsExceptionWrapper(ex);
            }
        }
        #endregion

        #region ToInt16
        /// <summary>
        /// 将字符串转换成等效的<see cref="Int16"/>值。
        /// </summary>
        /// <param name="current">字符串。</param>
        /// <returns><see cref="Int16"/>类型值。</returns>
        static public short ToInt16(this string current)
        {
            IfElse.Do(current.IsEmptyOrWriteSpaces(), trueExpr);
            try
            {
                return short.Parse(current);
            }
            catch (Exception ex)
            {
                throw new ArgumentsExceptionWrapper(ex);
            }
        }
        #endregion

        #region ToInt64
        /// <summary>
        /// 将字符串转换成等效的<see cref="Int64"/>值。
        /// </summary>
        /// <param name="current">字符串。</param>
        /// <returns><see cref="Int64"/>类型值。</returns>
        static public long ToInt64(this string current)
        {
            IfElse.Do(current.IsEmptyOrWriteSpaces(), trueExpr);
            try
            {
                return long.Parse(current);
            }
            catch (Exception ex)
            {
                throw new ArgumentsExceptionWrapper(ex);
            }
        }
        #endregion

        #region ToSingle
        /// <summary>
        /// 将字符串转换成等效的单精度浮点值。
        /// </summary>
        /// <param name="current">字符串。</param>
        /// <returns><see cref="float"/>类型值。</returns>
        static public float ToSingle(this string current)
        {
            IfElse.Do(current.IsEmptyOrWriteSpaces(), trueExpr);
            try
            {
                return float.Parse(current);
            }
            catch (Exception ex)
            {
                throw new ArgumentsExceptionWrapper(ex);
            }
        }
        #endregion

        #region ToDouble
        /// <summary>
        /// 将字符串转换成等效的双精度浮点值。
        /// </summary>
        /// <param name="current">字符串。</param>
        /// <returns><see cref="Double"/>类型值。</returns>
        static public double ToDouble(this string current)
        {
            IfElse.Do(current.IsEmptyOrWriteSpaces(), trueExpr);
            try
            {
                return double.Parse(current);
            }
            catch (Exception ex)
            {
                throw new ArgumentsExceptionWrapper(ex);
            }
        }
        #endregion

        #region ToDecimal
        /// <summary>
        /// 将字符串转换成等效的<see cref="Decimal"/>类型值。
        /// </summary>
        /// <param name="current">字符串。</param>
        /// <returns><see cref="Decimal"/>类型值。</returns>
        static public decimal ToDecimal(this string current)
        {
            IfElse.Do(current.IsEmptyOrWriteSpaces(), trueExpr);
            try
            {
                return decimal.Parse(current);
            }
            catch (Exception ex)
            {
                throw new ArgumentsExceptionWrapper(ex);
            }
        }
        #endregion

        #region ToEnum
        /// <summary>
        /// 将字符串转换成等效的枚举类型值。
        /// </summary>
        /// <typeparam name="TEnum">枚举的类型。</typeparam>
        /// <param name="current">字符串。</param>
        /// <returns><typeparamref name="TEnum"/>类型的枚举值。</returns>
        static public TEnum ToEnum<TEnum>(this string current)
        {
            IfElse.Do(current.IsEmptyOrWriteSpaces(), trueExpr);
            try
            {
                return Enum.Parse(TExpando<TEnum>.GetType(), current).ForceTo<TEnum>();
            }
            catch (Exception ex)
            {
                throw new ArgumentsExceptionWrapper(ex);
            }
        }
        #endregion

        #region GetBytes
        /// <summary>
        /// 使用编码器<paramref name="encoding"/>获取字符串的字节数组。
        /// </summary>
        /// <param name="current">字符串。</param>
        /// <param name="encoding"><see cref="Encoding"/>对象实例。</param>
        /// <returns><see cref="byte"/>[]。</returns>
        /// <remarks>
        /// 如果current等于空指针，则返回空指针。
        /// </remarks>
        static public byte[] GetBytes(this string current, Encoding encoding)
        {
            return current.IsNull() ? null : encoding.GetBytes(current);
        }
        #endregion

        #region GetString
        /// <summary>
        /// 使用编码器<paramref name="encoding"/>从字节数据中获取字符串。
        /// </summary>
        /// <param name="current">字节数据。</param>
        /// <param name="encoding"><see cref="Encoding"/>对象实例。</param>
        /// <returns>字符串。</returns>
        /// <remarks>如果<paramref name="current"/>为空指针，则返回空指针。</remarks>
        static public string GetString(this byte[] current, Encoding encoding)
        {
            return current.IsNull() ? null : encoding.GetString(current);
        }
        #endregion
    }
}

#region README

/*
 * ####     ©2014 Wang Yucai
 * 
 * ####     Contact me
 *                  
 *                  E-mail : Masterduner@Yeah.net
 *                  Mobile : 13621384677
 *                  QQ : 180261899
 */

#endregion